﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Core.DBUtility;
using Microhard.Core;
using Microhard.Plat.CRM.Customers.Interface;
using Microhard.Plat.CRM.Customers.Models;
using Microhard.Plat.CRM.Customers.DataAccess;
using Microhard.Plat.CRM.Customers.DTO;
using Microhard.Plat.CRM.Customers.Msg;
using Microhard.Plat.CRM.Customers.Sys;


namespace Microhard.Plat.CRM.Customers.Impl
{
    public class CusOrderDetailImpl : IOrderDetail
    {
        public bool IsCostCoinEnough(OrderDetailModelInServer model)
        {
            bool result = false;
            Microhard_Plat_CustomersEntities db = new Microhard_Plat_CustomersEntities();
            var prd = db.Products.SingleOrDefault(p => p.SN == model.SKU);
            //如果是赠品，则查询积分是否够用
            var allConins = db.OrderDetails.Where(o => o.OrderID == model.OrderID).Sum(x => x.Product.CostCoin);
            if (prd.IsPromotion.Value)
            {
                allConins += prd.CostCoin;
                //获得用户当前积分
                var mainOrder = db.CustomerOrders.SingleOrDefault(c => c.OrderID == model.OrderID);
                var restCoins = db.CustomerCoins.SingleOrDefault(c => c.CustomerID == mainOrder.CustomerID);
                if (restCoins.Coins >= allConins)
                {
                    result = true;
                }
            }
            return result;
        }
        public Microhard.Core.DBUtility.ExecutedResult Add(Models.OrderDetailModelInServer model)
        {
            ExecutedResult rtn = new ExecutedResult();
            Microhard_Plat_CustomersEntities db = new Microhard_Plat_CustomersEntities();

            var op = model.ToEntity();
            op.CreateTime = DateTime.Now;
            //获得产品
            var prd = db.Products.SingleOrDefault(p => p.SN == model.SKU);
            op.PrdID = prd.ProductID;
            op.PrdName = prd.Mark;

            try
            {
                db.OrderDetails.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, OrderMsg.AddDetailFailed);
                Log.Error("IOrderDetail.Add", ex);
            }
            return rtn;
        }

        public Microhard.Core.DBUtility.ExecutedResult Update(Models.OrderDetailModelInServer model)
        {
            ExecutedResult result = new ExecutedResult();
            Microhard_Plat_CustomersEntities db = new Microhard_Plat_CustomersEntities();
            var entity = db.OrderDetails.SingleOrDefault(l => l.OrderDetailID == model.OrderDetailID);
            if (entity != null)
            {
                entity.OrderQty = model.OrderQty;
                //获得产品
                var prd = db.Products.SingleOrDefault(s => s.SN == model.SKU);
                entity.PrdID = prd.ProductID;
                entity.PrdName = prd.Mark;
                entity.Price = model.Price;
                entity.SKU = prd.SN;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, OrderMsg.UpdateDetailFailed);
                Log.Error("IOrderDetail.Update", ex);
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            Microhard_Plat_CustomersEntities db = new Microhard_Plat_CustomersEntities();
            var entity = db.OrderDetails.Where(l => ids.Contains(l.OrderDetailID));
            var order = entity.First().Order;
            //是否在使用
            if (order.Status == (int)OrderStatus.Save)
            {
                foreach (var e in entity)
                {
                    db.OrderDetails.DeleteObject(e);
                }
            }
            else
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, OrderMsg.OrderDetailIsInUsing);
            }
            if (result.ErrorMsg.Count == 0)
            {
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, OrderMsg.DeleteDetailFailed);
                    Log.Error("IOrderDetail.Delete", ex);
                }
            }
            return result;
        }

        public Microhard.Core.DBUtility.ExecutedResult Delete(Models.OrderDetailModelInServer model)
        {
            throw new NotImplementedException();
        }

        public Models.OrderDetailModelInServer GetModelByID(Guid id)
        {
            throw new NotImplementedException();
        }

        public Models.OrderDetailModelInServer GetModelByModel(Models.OrderDetailModelInServer model)
        {
            throw new NotImplementedException();
        }

        public List<Models.OrderDetailModelInServer> Query(Models.OrderDetailModelInServer Model, int pageIndex, int pageSize, out int totalCount)
        {
            throw new NotImplementedException();
        }

        public List<Models.OrderDetailModelInServer> Query(Models.OrderDetailModelInServer Model)
        {
            List<Models.OrderDetailModelInServer> result = new List<OrderDetailModelInServer>();
            Microhard_Plat_CustomersEntities db = new Microhard_Plat_CustomersEntities();
            var entities = db.OrderDetails.Where(s => s.OrderID == Model.OrderID).OrderByDescending(s => s.CreateTime).ToList();
            if (entities.Count > 0)
            {
                result = entities.Select(p => p.ToModel()).ToList();
            }
            return result;
        }

        public Microhard.Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }
    }
}
